<?php

class Login_Model extends Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * Try to login user 
     */
    function Userlogin() {
        if (UserAccess::confirmUser($_POST['username'], $_POST['password'])) {
            $memberId = UserAccess::getMemberId($_POST['username'], $_POST['password']);

            UserAccess::createsessions($memberId, $_POST['username'], $_POST['password'], $_POST['rememberMe']);
            
            $this->UpdateUserPicks($_SESSION['memberId']);
            header('location: ' . URL . 'picks');
        }
        else
            header('location: ' . URL . 'login');
    }

    /**
     * Update Table 'Picks' for a given memberId, so there is a pick for every Match
     * @param type $memberId 
     */
    function UpdateUserPicks($memberId) {
        
        //Find MatchId the user has no Picks for
        $sthMatch = $this->database->prepare("SELECT matchId, defaultPriority FROM Matchs WHERE matchId NOT IN (SELECT matchId FROM Picks WHERE memberId=:memberId)");
        $sthMatch->execute(array(
            ':memberId' => $memberId,
        ));
        
        //Loop thruog matchId that has no picks
        while ($data = $sthMatch->fetch()) {
            $matchId = $data['matchId'];
            $defaultPriority = $data['defaultPriority'];
//echo "INSERT INTO Picks (memberId, matchId, picksResult, priority) VALUES (".$memberId." , ".$matchId.", 'Not Played', ".$defaultPriority.") <br />";
            $sthInsert = $this->database->prepare("INSERT INTO Picks (memberId, matchId, picksResult, priority) VALUES (:memberId, :matchId, 'Not Played', :defaultPriority)");            
            $sthInsert->execute(array(
                ':memberId' => $memberId,
                ':matchId' => $matchId,
                ':defaultPriority' => $defaultPriority
            ));
            
        }
    }
    
    /**
     * Change password for current user 
     */
    function ChangePassword() {
        if (UserAccess::checkLoggedin()) {
            UserAccess::changeUserPassword($_POST['currentpassword'], $_POST['newpassword'], $_POST['repeatnewpassword']);
        }
        header('location: ' . URL . 'login');
    }

    /**
     * Register new user 
     */
    function UserRegister() {
        if (!UserAccess::checkLoggedin()) {
            UserAccess::registerUser($_POST['username'], $_POST['email']);
        }

        header('Location: ' . URL . 'login');
    }

    /**
     * Change Viwer to Mobile viewer 
     */
    function SetMobileViewer() {
        UserAccess::SetMobileViewer(true);
        header('Location: ' . URL);
    }

    /**
     * Change Viwer to Desktop viewer 
     */
    function SetDesktopViewer() {
        UserAccess::SetMobileViewer(false);
        header('Location: ' . URL);
    }

    /**
     * Print Highscore table 
     */
    function PrintHighScoreTable() {
        $sth = $this->database->prepare("SELECT username, score, correctPicks, correctFirstPicks FROM Members ORDER BY score DESC, correctFirstPicks DESC, correctPicks DESC LIMIT 0, 20");
        $sth->execute();

        return $sth;
    }

}